package com.container.util;

import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;

import io.netty.util.concurrent.AbstractEventExecutor;
import io.netty.util.concurrent.FailedFuture;
import io.netty.util.concurrent.Future;

public class MyEventExecutorGroup extends AbstractEventExecutor {

    protected String namePrefix = "my-executor-";
    protected int maxThreads = 200;
    protected int minSpareThreads = 25;
    protected int maxIdleTime = 60000;
    protected int threadPriority = Thread.NORM_PRIORITY;

    protected ThreadPoolExecutor executor = null;//实际使用的线程池
    protected int maxQueueSize = Integer.MAX_VALUE;//任务队列最大长度
    private TaskQueue taskqueue = null;//任务队列
	private boolean daemon;

    public MyEventExecutorGroup() {
        startExecutor();
    }

    protected void startExecutor() {
        taskqueue = new TaskQueue(maxQueueSize);
        MyThreadFactory tf = new MyThreadFactory(namePrefix, true, getThreadPriority());//自定义的线程工厂类
        executor = new ThreadPoolExecutor(getMinSpareThreads(), getMaxThreads(), maxIdleTime, TimeUnit.MILLISECONDS, taskqueue, tf);
        taskqueue.setParent(executor);
    }

    protected void shutdownExecutor() {
        if ( executor != null ) executor.shutdownNow();
        executor = null;
        taskqueue = null;
    }

    @Override
    public void execute(Runnable task) {
        if ( executor != null ) {
            try {
                executor.execute(task);
            } catch (RejectedExecutionException rx) {
                if ( !( (TaskQueue) executor.getQueue()).force(task) ) throw new RejectedExecutionException("task queue is full.");
            }
        } else throw new IllegalStateException("MyEventExecutorGroup is not started.");
    }

    public int getThreadPriority() {
        return threadPriority;
    }

    public String getNamePrefix() {
        return namePrefix;
    }

    public int getMaxIdleTime() {
        return maxIdleTime;
    }

    public int getMaxThreads() {
        return maxThreads;
    }

    public int getMinSpareThreads() {
        return minSpareThreads;
    }

    public void setThreadPriority(int threadPriority) {
        this.threadPriority = threadPriority;
    }

    public void setDaemon(boolean daemon) {
        this.daemon = daemon;
    }

    public void setNamePrefix(String namePrefix) {
        this.namePrefix = namePrefix;
    }

    public void setMaxIdleTime(int maxIdleTime) {
        this.maxIdleTime = maxIdleTime;
        if (executor != null) {
            executor.setKeepAliveTime(maxIdleTime, TimeUnit.MILLISECONDS);
        }
    }

    public void setMaxThreads(int maxThreads) {
        this.maxThreads = maxThreads;
        if (executor != null) {
            executor.setMaximumPoolSize(maxThreads);
        }
    }

    public void setMinSpareThreads(int minSpareThreads) {
        this.minSpareThreads = minSpareThreads;
        if (executor != null) {
            executor.setCorePoolSize(minSpareThreads);
        }
    }

    public void setMaxQueueSize(int size) {
        this.maxQueueSize = size;
    }

    public int getMaxQueueSize() {
        return maxQueueSize;
    }

	@Override
	public boolean inEventLoop(Thread thread) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isShuttingDown() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Future<?> shutdownGracefully(long quietPeriod, long timeout, TimeUnit unit) {
		// TODO Auto-generated method stub
		return terminationFuture();
	}

	@Override
	public Future<?> terminationFuture() {
		// TODO Auto-generated method stub
		return new FailedFuture<Object>(this, new UnsupportedOperationException());
	}

	@Override
	public boolean isShutdown() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isTerminated() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	@Deprecated
	public void shutdown() {
		// TODO Auto-generated method stub
		
	}

	

}